package com.fzt.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fzt.project.common.convention.result.Result;
import com.fzt.project.common.convention.result.Results;
import com.fzt.project.dto.req.ShortLinkCreateReqDTO;
import com.fzt.project.dto.req.ShortLinkPageReqDTO;
import com.fzt.project.dto.req.ShortLinkUpdateReqDTO;
import com.fzt.project.dto.resp.GroupShortLinkCountRespDTO;
import com.fzt.project.dto.resp.ShortLinkCreateRespDTO;
import com.fzt.project.dto.resp.ShortLinkPageRespDTO;
import com.fzt.project.service.ShortLinkService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {
    private final ShortLinkService shortLinkService;


    /**
     * 短链接跳转功能
     */
    @GetMapping("/{short-uri}")
    public void restorUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response) {
        shortLinkService.restorUrl(shortUri, request, response);
    }

    /*
    创建短链接
     */
    @PostMapping("/api/short-link/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO reqDTO){
        return Results.success(shortLinkService.createShortLink(reqDTO));
    }

    /*
    分页查询短链接
     */
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO reqDTO){
        return Results.success(shortLinkService.pageShortLink(reqDTO));
    }

    /**
     * 查询短链接分组内数量
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<GroupShortLinkCountRespDTO>> listGroupShortLinkCount(@RequestParam("reqDTO") List<String> reqDTO){
        return Results.success(shortLinkService.listGroupShortLinkCount(reqDTO));
    }

    /**
     * 修改短链接
     */
    @PutMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO reqParam){
        shortLinkService.updateShortLink(reqParam);
        return Results.success();
    }
}
